#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import hashlib
from database import db_manager

def reset_test_password():
    """重置测试账号密码"""
    # 生成123456的SHA-256哈希
    password = '123456'
    password_hash = hashlib.sha256(password.encode('utf-8')).hexdigest()
    
    print(f'新密码哈希: {password_hash}')
    
    # 更新数据库中的密码
    try:
        affected = db_manager.execute_update(
            'UPDATE accounts SET password_hash = %s WHERE username = %s',
            (password_hash, 'test')
        )
        
        if affected > 0:
            print('测试账号密码重置成功')
        else:
            print('密码重置失败，账号不存在')
            
    except Exception as e:
        print(f'密码重置失败: {e}')
    
    # 验证密码
    result = db_manager.execute_query(
        'SELECT username, password_hash FROM accounts WHERE username = %s',
        ('test',)
    )
    
    if result:
        stored_hash = result[0]['password_hash']
        print(f'存储的密码哈希: {stored_hash}')
        print(f'密码验证: {password_hash == stored_hash}')
    else:
        print('账号不存在')

if __name__ == '__main__':
    reset_test_password()